﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.LinqToSQLDAL
{
    /// <summary>
    /// Linq To SQL DAL for events
    /// </summary>
    public class Events : IEvents
    {
        DB _db;

        /// <summary>
        /// Default constructor
        /// </summary>
        public Events()
        {
            _db = new DB();
        }

        /// <summary>
        /// Constructor used to pass in a datacontext
        /// </summary>
        /// <param name="db">datacontext for the DAL to use</param>
        public Events(DB db)
        {
            _db = db;
            if (_db == null)
                throw new InvalidOperationException("DB cannot be null.");
        }

        public IQueryable<Event> GetEvents()
        {
            return from e in _db.cs_Events
                   select new Event
                   {
                       ID = e.EventID,
                       StartDateTime = e.StartDateTime,
                       EndDateTime = (DateTime)e.EndDateTime,
                       Title = e.Title,
                       Description = e.Description,
                       LocationID = e.LocationID,
                       ImageID = e.ImageID,
                       CreationDate = e.CreationDate,
                       Author = e.Author
                   };
        }

        /// <summary>
        /// Method to get upcoming events
        /// </summary>
        /// <param name="days">Number of days in the future</param>
        /// <returns></returns>
        public IQueryable<Event> GetUpcomingEvents()
        {
            return from e in _db.cs_Events
                   where e.StartDateTime >= DateTime.Now
                   select new Event
                   {
                       ID = e.EventID,
                       StartDateTime = e.StartDateTime,
                       EndDateTime = (DateTime)e.EndDateTime,
                       Title = e.Title,
                       Description = e.Description,
                       LocationID = e.LocationID,
                       ImageID = e.ImageID,
                       CreationDate = e.CreationDate,
                       Author = e.Author
                   };
        }

        /// <summary>
        /// Method to get recent events
        /// </summary>
        /// <param name="days">Number of days in the past</param>
        /// <returns></returns>
        public IQueryable<Event> GetRecentEvents()
        {
            return from e in _db.cs_Events
                   where e.StartDateTime <= DateTime.Now
                   select new Event
                   {
                       ID = e.EventID,
                       StartDateTime = e.StartDateTime,
                       EndDateTime = (DateTime)e.EndDateTime,
                       Title = e.Title,
                       Description = e.Description,
                       LocationID = e.LocationID,
                       ImageID = e.ImageID,
                       CreationDate = e.CreationDate,
                       Author = e.Author
                   };
        }

        /// <summary>
        /// Method to get events on a certain date
        /// </summary>
        /// <param name="date">Date of the events</param>
        /// <returns></returns>
        public IQueryable<Event> GetEventsByDate(DateTime date)
        {
            return from e in _db.cs_Events
                   where e.StartDateTime.Date == date.Date
                   select new Event
                   {
                       ID = e.EventID,
                       StartDateTime = e.StartDateTime,
                       EndDateTime = (DateTime)e.EndDateTime,
                       Title = e.Title,
                       Description = e.Description,
                       LocationID = e.LocationID,
                       ImageID = e.ImageID,
                       CreationDate = e.CreationDate,
                       Author = e.Author
                   };
        }

        public Event GetEventByID(int id)
        {
            return (from e in _db.cs_Events
                    where e.EventID == id
                    select new Event
                    {
                        ID = e.EventID,
                       StartDateTime = e.StartDateTime,
                       EndDateTime = (DateTime)e.EndDateTime,
                       Title = e.Title,
                       Description = e.Description,
                       LocationID = e.LocationID,
                       ImageID = e.ImageID,
                       CreationDate = e.CreationDate,
                       Author = e.Author
                    }).SingleOrDefault();
        }

        public void Save(Event eventToSave)
        {
            using (DB db = new DB())
            {
                cs_Event dbEvent = db.cs_Events.Where(e => e.EventID == eventToSave.ID).SingleOrDefault();
                bool isNew = false;
                if (dbEvent == null)
                {
                    dbEvent = new cs_Event();
                    isNew = true;
                }

                dbEvent.StartDateTime = eventToSave.StartDateTime;
                dbEvent.EndDateTime = eventToSave.EndDateTime;
                dbEvent.Title = eventToSave.Title;
                dbEvent.Description = eventToSave.Description;
                dbEvent.LocationID = eventToSave.LocationID;
                dbEvent.ImageID = eventToSave.ImageID;
                dbEvent.CreationDate = eventToSave.CreationDate;

                if (isNew)
                    db.cs_Events.InsertOnSubmit(dbEvent);

                db.SubmitChanges();
            }
        }

        public void Delete(int id)
        {
            cs_Event dbEvent = _db.cs_Events.Where(e => e.EventID == id).SingleOrDefault();

            if (dbEvent != null)
            {
                _db.cs_Events.DeleteOnSubmit(dbEvent);
                _db.SubmitChanges();
            }
        }
    }
}
